package com.unicom.priceboard.thread;

import java.util.HashMap;

import com.unicom.priceboard.data.hose.LeReader;
import com.unicom.priceboard.data.vo.LeElement;
import com.unicom.priceboard.util.Constants;
import com.unicom.priceboard.util.DataPath;
import com.unicom.priceboard.util.ReadFileUtil;
import com.unicom.priceboard.util.Util;

public class LeThread implements Runnable{
	private int curday=-1;
	private int prevday=-1;
	private int wait = 60; //seconds
	
	private String leSrcPath = "";
	
	public static boolean leFlag = false;
	
	//List for T2 Price and T2 Vol
	public static HashMap<Short, LeElement> T2_lastest_object = new HashMap<Short, LeElement>();
	public static HashMap<Short, LeElement> T1_lastest_object = new HashMap<Short, LeElement>();
	
	//List for intra day
	public static volatile HashMap<String, LeElement> matchingList = new HashMap<String, LeElement>();
	
	private LeReader parseLe =new LeReader();
	
	public LeThread(){
		leSrcPath = DataPath.basePath + DataPath.leSrcPath;
		this.wait=Integer.parseInt(DataPath.hoseWait);
	}
	public void run() {
		while(true)
		{
			try			
			{	
				if(TimerThread.CURRENT_DAY_OF_WEEK!=1 && TimerThread.CURRENT_DAY_OF_WEEK!=7) {
					if(TimerThread.CURRENT_TIME>=Constants.LE_READ_START &&TimerThread.CURRENT_TIME<Constants.LE_READ_END) {
						if(ReadFileUtil.isUpToDate(Util.getCurrentDataPath(leSrcPath))){
								leFlag=false;
								curday=TimerThread.CURRENT_DAY_OF_WEEK;
								if(curday!=prevday){
									T1_lastest_object.clear();
									T2_lastest_object.clear();
									matchingList.clear();
									prevday=curday;										
								}
						parseLe.parseLe(leSrcPath);		
						leFlag=true;
						}//end check modify
						
					}else if(T2_lastest_object.size()==0||T1_lastest_object.size()==0) {
						leFlag=false;
						parseLe.parseLe(leSrcPath);
						leFlag=true;
					}
				}else if(T2_lastest_object.size()==0||T1_lastest_object.size()==0) {					 
					leFlag=false;
					parseLe.parseLe(leSrcPath);
					leFlag=true;
					
				}
			}catch(Exception e)
			{
				//logger.debug("*****parsing LE process error :\n"+e.getMessage());
			}
			try {
				//logger.debug("***** parsing LE.DAT Wait for " + wait + " seconds");
				Thread.sleep(1000*wait);
			} catch (Exception e) {
				//logger.debug("Exception: " + e.getMessage());
			}
		}

	}
}
